package globulin;

import jgame.*;
import jgame.platform.JGEngine.*;
import jgame.impl.JGEngineInterface.*;

/**
 * Manages the power up object
 * 
 * @author Jonathan Horvath
 */
public class PowerUp extends JGObject {

    /**
     * Used to change the direction of the Power Up erradicately
     */
    private int timer = 0;

    /**
     * Constructor
     *
     * @param powerupType
     * @param xPosition
     */
    public PowerUp(int xPosition) {
        // String name, boolean unique_id, double x, double y, int collisionID,
        // String gfxName, double xSpeed, double ySpeed
        super("powerup", true, xPosition, -8, Globulin.POWERUP_COLID, "powerup", 0.1, 0.1);
    }

    /**
     * Handles the move function for the powerup. 
     */
    @Override
    public void move() {
        // Increments the timer
        timer++;

        // If the powerup hits a wall, it will reverse direction
        if ((x < 0 && xspeed < 0) || (x > eng.pfWidth() - tilewidth && xspeed > 0))
            this.setSpeed(-xspeed, yspeed);

        // Changes the speed at irregular intervals
        if (timer % eng.random(0, eng.getFrameRate(), 1) == 0)
            setSpeed(Math.min(1, Math.cos(timer) + Math.sin(timer) + xspeed),
                    Math.min(1, Math.abs(Math.cos(timer) + Math.sin(timer) + yspeed)));

        // If the powerup goes off of the bottom of the screen, it despawns
        if (y > eng.pfHeight()) 
            this.remove();
    }
}
